do "E:\yungyu\lottery_baby\do\set_environment.do"

clear
set more off

global control1 "i.pers_gender i.houhld_resid_cd pers_income_pre1 pers_asset_pre1 pers_earnings_pre1 pers_labor_pre1 married_pre1"
global control2 "totbaby_pre1"
global control3 "pre*redeem*"

use "$wdata\project_lottery_fertility_b3a8_first_5k.dta", clear

drop if prize10M > 5

sum totbaby if current == 0 & norm == -1
loc m0 = r(mean)
sum totbaby if current == 0 & norm == 6
loc m1 = r(mean)
loc bt = `m1' - `m0'

replace norm_year = norm_year + 3

reghdfe totbaby currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3, a(norm#current year) cl(houseid)
outreg2 using "$table/temp/FigC1.xls" , append bd(3) sd(3) nocon keep(currentxprize*) ctitle(B3A8) addtext(Baseline Trend, `bt')

import delimited using "$table/temp/FigC1.txt", clear stringcols(_all) stripquotes(yes)
export excel "$table/Tables.xlsx", sheet("FigC1") sheetreplace

import excel using "$table/Tables.xlsx", clear sheet(FigC1) first

keep in 4/25
keep v2
gen norm = -4 + ceil(_n/2)
replace norm = norm + 1 if norm >= -1

loc N = _N + 2
set obs `N'
replace norm = -1 if norm == .

gen type = mod(_n+1,2)
reshape wide v7, i(norm) j(type)

rename v20 coef
rename v21 se

foreach x in coef se{
	replace `x' = subinstr(`x',"*","",.)
	replace `x' = subinstr(`x',"(","",.)
	replace `x' = subinstr(`x',")","",.)
	destring `x', replace
	recode `x' . = 0
}

gen upper = coef + 1.96 * se
gen lower = coef - 1.96 * se


twoway 	(connect coef norm, ms(O) color(black)) ///
		(rline upper lower norm, lp(dash) color(gs8)), ///
		legend(order(1 "Point Estimate" 2 "95% CI")) ///
		xtitle(Years from Lottery Prize) xlabel(-3(1)6) ///
		ytitle(Effect on Cumulative Number of Children) ylabel(, angle(0) format(%4.2f)) ///
		scheme(s1color) yline(0, lc(black))
		
graph export "$folder/figure/FigC1.eps", as(eps) replace
